package com.yxw.timeDemo;
import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;

import java.math.BigDecimal;
import java.util.logging.Logger;
/**
 * Client 网络IO事件处理
 * @author xwalker
 *
 */
public class TimeClientHandler extends ChannelHandlerAdapter {
    private static final Logger logger=Logger.getLogger(TimeClientHandler.class.getName());
    private  User user;
    public TimeClientHandler(){
        user = new User();
        user.setUserId(new BigDecimal(1));
        user.setUserName("YXW");
        user.setCardId("360732199010012545");
        user.setMobile("13556567972");
    }
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        Request request = new Request();
        request.setRequest(user);
        ctx.writeAndFlush(request);
        System.out.println("客户端active");
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg)
            throws Exception {
        System.out.println("客户端收到服务器响应数据");
        Response response = (Response) msg;
        User user = response.getResult();
        System.out.println("Now is:"+user.toString());
    }

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
        ctx.flush();
        System.out.println("客户端收到服务器响应数据处理完成");
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
            throws Exception {
        logger.warning("Unexpected exception from downstream:"+cause.getMessage());
        ctx.close();
        System.out.println("客户端异常退出");
    }
}